package com.sky.controller.admin;

import com.sky.DTO.EmployeeDTO;
import com.sky.constant.JwtClaimsConstant;
import com.sky.context.ThreadLocalContext;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.pageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关的功能接口")//描述该(功能类)Controller的功能
@CrossOrigin//解决跨域问题
public class EmployeeController {
    @ApiModelProperty(value = "员工业务逻辑service")
    @Autowired
    private EmployeeService employeeService;
    @ApiModelProperty(value = "jwt配置属性")
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录功能接口")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder().id(employee.getId()).userName(employee.getUsername()).name(employee.getName()).token(token).build();//lombock提供的@Builder注解，可以自动生成builder方法,构建对象

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation("员工退出功能接口")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success("退出成功");
    }

    /**
     * 新增员工
     */
    @PostMapping
    @ApiOperation("新增员工功能接口")
    public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        //业务:新增员工
        employeeService.save(employeeDTO);
        //返回统一的响应结果Result
        return Result.success("新增员工成功");

    }

    /**
     * 员工条件分页查询
     */
    @ApiOperation("员工条件分页查询功能接口")
    @GetMapping("/page")
    public Result<pageResultVO<Employee>> page(EmployeePageQueryDTO employeePageQueryDTO) {
        //记录日志
        log.info("员工条件分页查询：{}", employeePageQueryDTO);
        //业务:员工条件分页查询
        pageResultVO<Employee> pageResultVO = employeeService.page(employeePageQueryDTO);
        //返回统一的响应结果Result
        return Result.success(pageResultVO);
    }

    /**
     * 启用和禁用员工账号
     * <p>
     * 状态：0-禁用，1-启用<br>
     * <b>业务规则：</b>
     * <p>
     * - 可以对状态为“启用” 的员工账号进行“禁用”操作<br>
     * - 可以对状态为“禁用”的员工账号进行“启用”操作<br>
     * - 状态为“禁用”的员工账号不能登录系统<br>
     * <p>
     * Path： /admin/employee/status/{status}
     * <p>
     * Method： POST
     */
    @ApiOperation("根据id启用和禁用员工账号功能接口")
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable("status") Integer status, Long id) {
        //记录日志
        log.info("启用和禁用员工账号,0-禁用,1-启用：status:{},id:{}", status, id);
        //业务:启用和禁用员工账号
        employeeService.updateStatus(status, id);
        return Result.success("禁用或者启用成功");//返回统一的响应结果Result

    }


    /**
     * 修改员工的查询回显和修改员工信息的功能接口<br>
     * **Path：** /admin/employee
     * <p>
     * **Method：** PUT
     */
    @PutMapping
    public Result<String> update(@RequestBody EmployeeDTO employeeDTO) {
        //记录日志
        log.info("修改员工信息：{}", employeeDTO);
        //业务:修改员工信息
        employeeService.update(employeeDTO);
        //返回统一的响应结果Result
        return Result.success("修改员工信息成功");
    }

    /**
     * 根据id查询员工,用于查询回显
     * <p>
     * Path： /admin/employee/{id}
     * <p>
     * Method： GET
     */
    @ApiOperation("根据id查询员工功能接口")
    @GetMapping("/{id}")
    public Result<Employee> get(@PathVariable Long id) {
        //记录日志
        log.info("根据id查询员工：id:{}", id);
        //业务:根据id查询员工
        Employee employee = employeeService.get(id);
        //返回统一的响应结果Result
        return Result.success(employee);

    }


    /**
     * 根据id删除员工
     * <p>
     * Path： /admin/employee/{id}
     * <p>
     * Method： DELETE
     */
    @ApiOperation("根据id删除员工功能接口")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        //记录日志
        log.info("根据id删除员工：id:{}", id);
        //业务:根据id删除员工
        employeeService.delete(id);
        //返回统一的响应结果Result
        return Result.success("删除员工成功");

    }


    /**
     * 修改密码
     * <p>
     * **Path：** /admin/employee/editPassword
     * <p>
     * **Method：** PUT
     */
    @ApiOperation("修改密码功能接口")
    @PutMapping("/editPassword")
    public Result<String> editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        //1.使用threadLocal获取当前登录的用户的id.设置到passwordEditDTO中
        passwordEditDTO.setEmpId(ThreadLocalContext.getCurrentLocalUserId());
        //记录日志
        log.info("修改密码：{}", passwordEditDTO);
        //业务:修改密码
        employeeService.editPassword(passwordEditDTO);
        //返回统一的响应结果Result
        return Result.success("修改密码成功");
    }



}
